package income

import (
	"week13/log"
	"time"
	Repo "week13/internal/data/income"
)

var SettleResposity Repo.SettleRepository = Repo.SettleDao{}
/*
  查找可结算的收入记录进行结算。
  结算流程：
	设置结算状态
	增加渠道的账号余额
*/
func SettleIncome()  {

	timeStr := time.Now().Format("2006-01-02")
	t, _ := time.ParseInLocation("2006-01-02", timeStr,time.Local)
	timeNumber := t.Unix()
	endTime := int(timeNumber)
	startTime := endTime-86400
	page := 1
	pageSize := 5000
	log.Info("start settleIncome",",settleBegin:",startTime,",settleEnd:",endTime)
	for{
		incomes,err := Repo.GetCanSettleIncome([]int{startTime,endTime},0,page,pageSize)
		page += 1
		if err != nil {
			log.Error("fetch incomes err when settle income")
			return
		}
		if len(incomes) < 1 {
			log.Info("fetch empty income where settle income")
			return
		}
		SettleResposity.SettleIncome(incomes)
		incomes = nil
	}

}
